import sys
import win32gui
import win32con


# get_all_windows()：获取当前系统中所有窗口的句柄。
# get_son_windows(parent)：获取指定窗口的所有子窗口句柄。
# get_title(hwnd)：根据窗口句柄获取窗口标题。
# get_classname(hwnd)：根据窗口句柄获取窗口类名。
# get_hwnd_from_name(name)：根据窗口标题获取窗口句柄。

def get_all_windows():
    hWnd_list = []
    win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWnd_list)
    # print(hWnd_list)
    return hWnd_list

def get_son_windows(parent):
    hwnd_child_list=[]
    win32gui.EnumChildWindows(parent,lambda hwnd,param:param.append(hwnd),hwnd_child_list)
    print(hwnd_child_list)
    return hwnd_child_list

def get_title(hwnd):
    title = win32gui.GetWindowText(hwnd)
    print('窗口标题:%s' % (title))
    return title

def get_classname(hwnd):
    clasname = win32gui.GetClassName(hwnd)
    print('窗口类名:%s' % (clasname))
    return clasname

def get_control_type(hwnd):
    # 获取窗口类名
    class_name = win32gui.GetClassName(hwnd)
    # 返回窗口类名作为控件类型
    return class_name

def get_control_rect(hwnd):
    # 获取窗口位置
    rect = win32gui.GetWindowRect(hwnd)
    # 返回窗口位置信息
    return rect


#根据窗口名称获取句柄
def get_hwnd_from_name(name):
    hWnd_list = get_all_windows()
    for hwd in hWnd_list:
        title = get_title(hwd)
        if title == name:
            return hwd

def get_window_info(hwnd_list):
    window_info_list = []
    for hwnd in hwnd_list:
        title = win32gui.GetWindowText(hwnd)
        control_type = get_control_type(hwnd)
        control_rect = get_control_rect(hwnd)
        width = control_rect[2] - control_rect[0]
        height = control_rect[3] - control_rect[1]
        # 计算控件中心点作为最优点击位置
        optimal_click_position = ((control_rect[0] + control_rect[2]) // 2, (control_rect[1] + control_rect[3]) // 2)
        window_info = {
            "窗口标题": title,
            "窗口句柄": hwnd,
            "控件类型": control_type,
            "控件位置": control_rect,
            "控件宽度": width,
            "控件高度": height,
            "最优点击位置": optimal_click_position
        }
        window_info_list.append(window_info)
    return window_info_list

# 示例调用
# hwnd_list = [1639662, 2426372, 658858, 658932, 4522358]
# window_info_list = get_window_info(hwnd_list)
# for window_info in window_info_list:
#     print(window_info)